﻿using Ping.Core;

namespace Ping.Client.ViewModels
{
	public sealed class TracerouteModel
	{
		private readonly PingModel[] _results;

		public TracerouteModel(TracerouteResult result)
		{
			_results = new PingModel[result.Count];
			for (int i = 0; i < result.Count; ++i)
			{
				_results[i] = new PingModel(i, result.Hops[i]);
			}
		}

		public PingModel[] Rows
		{
			get { return _results; }
		}
	}

	public sealed class PingModel
	{
		private readonly int _index;
		private readonly PingResult _result;

		public PingModel(int index, PingResult result)
		{
			_index = index;
			_result = result;
		}

		public bool IsValid
		{
			get { return _result.Sender.IsValid; }
		}

		public string Row
		{
			get
			{
				return IsValid
						? string.Format("[{0}]: {1}, {2}", _index, Address, Ttl)
						: string.Format("[{0}]: {1}", _index, Address);
			}
		}

		public string Address
		{
			get { return _result.Sender.IsValid ? _result.Sender.ToString() : "n/a"; }
		}

		public string Ttl
		{
			get { return IsValid ? string.Format("{0} ms", (int)_result.Rtt.TotalMilliseconds) : string.Empty; }
		}
	}
}
